package com.yimils.core.hardware.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yimils.core.common.dto.GetQueryParams;
import com.yimils.core.common.dto.RspBase;
import com.yimils.core.hardware.dto.ReqAddHwRetail;
import com.yimils.core.hardware.entity.RetailEntity;
import com.yimils.core.hardware.service.RetailService;
import com.yimils.core.hardware.vo.RetailVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/hardware")
public class RetailController {
    @Resource(name = "retailService")
    private RetailService retailService;

    @GetMapping("/retail/heartbeat")
    public RspBase heartbeatCallback(String macno, Integer electricity) {
        System.out.println("heartbeatCallback" + " macno: " + macno + " electricity: " + electricity);

        return RspBase.OK();
    }

    @PostMapping("/retail")
    public RspBase createHWRetail(@RequestBody ReqAddHwRetail dto) {
        if (retailService.createHWRetail(dto)) {
            return RspBase.OK("操作成功");
        }
        return RspBase.ERROR(400, "操作失败");
    }

    @GetMapping("/retailVo/{id}")
    public RspBase getRetailVoById(@PathVariable Long id) {
        RspBase rsp = RspBase.OK();
        RetailVo retailVo = retailService.getRetailVoById(id);
        if (retailVo != null) {
            rsp.put("data", retailVo);
        }
        return rsp;
    }

    @GetMapping("/retail/{id}")
    public RspBase getRetailById(@PathVariable Long id) {
        RspBase rsp = RspBase.OK();
        RetailEntity entity  = retailService.getById(id);
        if (entity != null) {
            rsp.put("data", entity);
        }
        return rsp;
    }

    @PutMapping("/retail")
    public RspBase updateRetail(@RequestBody RetailEntity entity) {
        if (retailService.modify(entity)) {
            return RspBase.OK("操作成功");
        }
        return RspBase.ERROR(400, "操作失败");
    }

    @GetMapping("/retails")
    public RspBase getHWRetails(GetQueryParams queryParams) {
        RspBase rsp = RspBase.OK();
        IPage<RetailVo> list = retailService.getListForPage(queryParams);
        rsp.put("data", list);
        return rsp;
    }
}
